package com.uphone.magicleafteacher.pro.fragment.mine;


import android.arch.lifecycle.LifecycleOwner;
import android.text.TextUtils;

import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.radish.baselibrary.utils.LogUtils;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;
import com.uphone.magicleafteacher.MyApp;
import com.uphone.magicleafteacher.base.mvp.BasePAV;
import com.uphone.magicleafteacher.http.ApiService;
import com.uphone.magicleafteacher.http.FailureConsumer;
import com.uphone.magicleafteacher.http.RequestBodyUtil;
import com.uphone.magicleafteacher.http.RxSchedulers;
import com.uphone.magicleafteacher.http.SuccessfulConsumer;
import com.uphone.magicleafteacher.util.CommonUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import okhttp3.MultipartBody;

public class MinePeresenter extends BasePAV<MineContract.View> implements MineContract.Presenter {
    @Inject
    public MinePeresenter() {

    }

    @Override
    public void loadNetData() {

    }

    public void uploadImgFile(File file) {
        MultipartBody.Part part = RequestBodyUtil.filesToMultipartBodyPart("file", file);
        MyApp.apiService(ApiService.class)
                .uploadImgFile(part)
                .compose(RxSchedulers.io_main())
                .doOnSubscribe(d -> {
                    mView.showLoading();
                })
                .doFinally(() -> {
                    mView.closeLoading();
                })
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from((LifecycleOwner) mView)))
                .subscribe(new SuccessfulConsumer() {
                    @Override
                    public void success(String jsonObject) {
                        String filePath = SuccessfulConsumer.getKey(jsonObject, "filePath");
                        if (!TextUtils.isEmpty(filePath)) {
                            modTeacherImg(filePath);
                        } else {
                            ToastUtils.showShort("上传头像失败");
                        }
                    }
                }, new FailureConsumer());
    }

    private void modTeacherImg(String url) {
        MyApp.apiService(ApiService.class)
                .modTeacherImg(CommonUtil.getTeacherId(),url)
                .compose(RxSchedulers.io_main())
                .doOnSubscribe(d -> {
                    mView.showLoading();
                })
                .doFinally(() -> {
                    mView.closeLoading();
                })
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from((LifecycleOwner) mView)))
                .subscribe(new SuccessfulConsumer() {
                    @Override
                    public void success(String jsonObject) {
                        mView.updateFile(url);
                    }
                }, new FailureConsumer());
    }
}
